package com.example.figurehecheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.figurehecheng.common.config.checkObjAllFieldsIsNull;
import com.example.figurehecheng.common.constant.MessageConstant;
import com.example.figurehecheng.common.context.BaseContext;
import com.example.figurehecheng.common.exception.AccountExist;
import com.example.figurehecheng.common.result.PageResult;
import com.example.figurehecheng.common.utils.AliOssUtil;
import com.example.figurehecheng.domain.dto.FoodtypeDTO;
import com.example.figurehecheng.domain.entity.Food;
import com.example.figurehecheng.domain.entity.Foodtype;
import com.example.figurehecheng.domain.entity.Image;
import com.example.figurehecheng.domain.entity.Merchant;
import com.example.figurehecheng.domain.vo.FoodVO;
import com.example.figurehecheng.domain.vo.FoodtypeVO;
import com.example.figurehecheng.mapper.FoodtypeMapper;
import com.example.figurehecheng.service.IFoodtypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.figurehecheng.service.IImageService;
import com.example.figurehecheng.service.IMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 宇科工作室
 * @since 2024-10-18
 */
@Service
public class FoodtypeServiceImpl extends ServiceImpl<FoodtypeMapper, Foodtype> implements IFoodtypeService {


    @Autowired
    private IImageService imageService;
    @Autowired
    private IMerchantService iMerchantService;


    @Override
    public PageResult getDishtypeAll(String name, Integer pageNum, Integer pageSize) {
        //分页查询获取对应的菜品
        Page<Foodtype> pages = Page.of(pageNum, pageSize);
        Page<Foodtype> foodtype = lambdaQuery().
                eq(Foodtype::getCanteenId, BaseContext.getCurrentId())
                .like(ObjectUtil.isNotEmpty(name),Foodtype::getNameType, name)
                .page(pages);

        List<FoodtypeVO> foodVOS = new ArrayList<>();


        for (int i=0;i< foodtype.getRecords().size();i++){
            System.out.println(foodtype.getRecords());
                    FoodtypeVO foodVO = BeanUtil.copyProperties(foodtype.getRecords().get(i), FoodtypeVO.class);
                    foodVOS.add(foodVO);
        }
        return   PageResult.builder()
                .total(foodtype.getTotal())
                .records(foodVOS)
                .pages(foodtype.getPages())
                .build();
    }

    @Override
    public void updateDish(FoodtypeDTO foodtypeDto) {

        //查看对象所有信息是否为空
        if (checkObjAllFieldsIsNull.objCheckIsNull(foodtypeDto)) {
            throw new AccountExist(MessageConstant.COUPON_NOT_NULL);
        }

        lambdaUpdate().set(Foodtype::getNameType, foodtypeDto.getNameType())
                .set(Foodtype::getSort, foodtypeDto.getSort())
                .eq(Foodtype::getCanteenId, BaseContext.getCurrentId())
                .update();


    }

    @Override
    public void deleteDish(FoodtypeDTO foodtypeDTO) {
        remove(new QueryWrapper<Foodtype>()
                .eq("id", foodtypeDTO.getId())
                .eq("canteen_id", BaseContext.getCurrentId()));

    }

    @Override
    public void addDish(FoodtypeDTO foodtypeDTO) {
        if (!ObjectUtil.isAllNotEmpty(foodtypeDTO.getNameType(),foodtypeDTO.getSort())) {
            throw new AccountExist(MessageConstant.COUPON_NOT_NULL);
        }
        //先添加菜品
        Foodtype food = BeanUtil.copyProperties(foodtypeDTO, Foodtype.class);
        food.setCanteenId(BaseContext.getCurrentId().intValue());
        save(food);

    }
}
